package org.qinys.mongodb.dao.impl;

import java.util.List;
import java.util.Set;

import org.qinys.mongodb.dao.WeiboDao;
import org.qinys.mongodb.model.WeiboInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.mongodb.DB;
import com.mongodb.diagnostics.logging.Logger;
import com.mongodb.diagnostics.logging.Loggers;
@Repository
public class WeiboDaoImpl implements WeiboDao {

	public static final Logger logger = Loggers.getLogger("WeiboDaoImpl");  
	  
    @Autowired  
    private MongoTemplate mongoTemplate;
	
	public void _test() {
		DB db = this.mongoTemplate.getDb();  
		logger.info("db=" + db.toString());
		 Set<String> colls = this.mongoTemplate.getCollectionNames();  
	        for (String coll : colls) {  
	            logger.info("CollectionName=" + coll);  
	        }  
	}

	public void createCollection() {
		if (!this.mongoTemplate.collectionExists(WeiboInfo.class)) {  
            this.mongoTemplate.createCollection(WeiboInfo.class);  
        } 
	}

	public List<WeiboInfo> findList(Integer limit) {
		Query query = new Query();
        query.with(new Sort(new Order(Direction.ASC, "_id")));
        if (limit!=null) {
        	query.limit(limit);  
		}
        return this.mongoTemplate.find(query, WeiboInfo.class);
	}

	public List<WeiboInfo> findListBySchool(String school, Integer limit) {
		Query query = new Query();
		query.with(new Sort(new Order(Direction.ASC, "_id")));
        query.addCriteria(new Criteria("school").is(school));
        if (limit!=null) {
        	query.limit(limit);  
		}
        return this.mongoTemplate.find(query, WeiboInfo.class);
	}

	public WeiboInfo findOne(String id) {
		Query query = new Query();
		query.with(new Sort(new Order(Direction.ASC, "_id")));
        query.addCriteria(new Criteria("_id").is(id));
        return this.mongoTemplate.findOne(query, WeiboInfo.class);
	}

	public WeiboInfo findOneByWeiboId(String weiboId) {
		Query query = new Query();
		query.with(new Sort(new Order(Direction.ASC, "_id")));
        query.addCriteria(new Criteria("weiboId").is(weiboId));
        return this.mongoTemplate.findOne(query, WeiboInfo.class);
	}

	public int insert(WeiboInfo weiboInfo) {
		if (weiboInfo==null||StringUtils.isEmpty(weiboInfo.getWeiboId())) {
			return 0;
		}
		if (this.findOneByWeiboId(weiboInfo.getWeiboId())!=null) {
			return 0;
		}
		this.mongoTemplate.insert(weiboInfo);
		return 1;
	}

	public int update(WeiboInfo weiboInfo) {
		if (weiboInfo==null||StringUtils.isEmpty(weiboInfo.getWeiboId())) {
			logger.info("参数错误");
			return 0;
		}
		Query query = new Query();
        query.addCriteria(new Criteria("weiboId").is(weiboInfo.getWeiboId()));
        Update update = new Update();
        if (StringUtils.isEmpty(weiboInfo.getWeiboId())) {
        	update.set("weiboId",weiboInfo.getWeiboId());//账号id
		}
        if (StringUtils.isEmpty(weiboInfo.getNickName())) {
        	update.set("nickName",weiboInfo.getNickName());//昵称
        }
        if (StringUtils.isEmpty(weiboInfo.getSex())) {
        	update.set("sex",weiboInfo.getSex());//性别
        }
        if (StringUtils.isEmpty(weiboInfo.getTags())) {
        	update.set("tags",weiboInfo.getTags());//标签
        }
        if (StringUtils.isEmpty(weiboInfo.getAddress())) {
        	update.set("address",weiboInfo.getAddress());//地址
        }
        if (StringUtils.isEmpty(weiboInfo.getBirthday())) {
        	update.set("birthday",weiboInfo.getBirthday());//生日
        }
        if (StringUtils.isEmpty(weiboInfo.getSchool())) {
        	update.set("school",weiboInfo.getSchool());//毕业学校
        }
        if (StringUtils.isEmpty(weiboInfo.getFollowers())||weiboInfo.getFollowers().length==0) {
        	update.set("followers",weiboInfo.getFollowers());//关注者
        }
        if (StringUtils.isEmpty(weiboInfo.getFans())||weiboInfo.getFans().length==0) {
        	update.set("fans",weiboInfo.getFans());//粉丝
        }
        this.mongoTemplate.updateFirst(query, update, WeiboInfo.class);
        return 1;
	}

	
	public int delete(WeiboInfo weiboInfo) {
		if (weiboInfo==null||StringUtils.isEmpty(weiboInfo.getWeiboId())) {
			return 0;
		}
		if (this.findOneByWeiboId(weiboInfo.getWeiboId())==null) {
			return 0;
		}
		Query query = new Query();
		query.addCriteria(new Criteria("weiboId").is(weiboInfo.getWeiboId()));
		this.mongoTemplate.remove(query, WeiboInfo.class);
		return 1;
	}

}
